<?php

namespace app\admin\model;

use think\Model;

/**
 * 涨知识阅读量
 * @package app\admin\model
 */
class ArticleViews extends Model
{
    /**
     * 统计文章阅读人数
     * @param array $where
     * @return int
     */
    public static function countArticleViews($where)
    {
        return self::where($where)->count('distinct uid');
    }

    /**
     * 统计文章阅读次数
     * @param array $where
     * @return mixed
     */
    public static function countArticleViewsNums($where)
    {
        return self::where($where)->sum('nums');
    }

    /**
     * 统计当天文章阅读次数
     * @param array $where
     * @return mixed
     */
    public static function countTodayArticleViewsNums($where)
    {
        return self::whereDay('create_time')
            ->where($where)
            ->sum('nums');
    }

    /**
     * 统计当月文章阅读次数
     * @param array $where
     * @return mixed
     */
    public static function countMonthArticleViewsNums($where = [])
    {
        return self::whereMonth('create_time')
            ->where($where)
            ->sum('nums');
    }

    /**
     * 统计文章阅读量
     * @param $where
     * @return float
     */
    public static function sumArticleViews($where)
    {
        return self::where($where)->sum('nums');
    }

    /**
     * 统计文章当日阅读人数
     * @param array $where
     * @return int
     */
    public static function countTodayArticlePeopleViews($where)
    {
        return self::where($where)->whereDay('create_time')->count('distinct uid');
    }

    /**
     * 统计文章昨日阅读人数
     * @param array $where
     * @return int
     */
    public static function countYesTodayArticlePeopleViews($where)
    {
        return self::where($where)->whereDay('create_time')->count('distinct uid');
    }

    /**
     * 统计文章本月阅读人数
     * @param array $where
     * @return int
     */
    public static function countMonthArticlePeopleViews($where)
    {
        return self::where($where)->whereMonth('create_time')->count('distinct uid');
    }

    /**
     * 访问人身份数据
     * @param $where
     * @return array
     */
    public static function getArticlePeopleList($where)
    {
        $data = self::where($where)->distinct(true)->field('uid')->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 文章访问人累计
     * @param $where
     * @return int
     */
    public static function countArticleViewsPeople($where)
    {
        return self::where($where)->count('distinct uid');
    }

    /**
     * 其他文章分类访问人
     * @param $where
     * @return array
     */
    public static function getOtherArticleViewPeople($where)
    {
        $data = self::fieldRaw("COUNT(distinct uid) nums,types")
            ->where($where)
            ->group('types')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 文章访问总数（折线图）
     * @param $startTime
     * @param $endTime
     * @param $where
     * @return array
     */
    public static function getArticleViewsLine($startTime, $endTime, $where)
    {
        $data = self::fieldRaw("COUNT(distinct uid) total,create_time")
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->where($where)
//            ->fetchSql()
            ->group('create_time')
            ->select();
//        var_dump($data);

        return $data ? $data->toArray() : [];
    }


}